package com.dome.dao;

import com.dome.bean.UserBean;
import com.dome.utils.JDBCutil;

import java.io.IOException;
import java.sql.*;

public class UserDao {
    public int insert(UserBean bean) throws SQLException, IOException, ClassNotFoundException {
        Connection conn= JDBCutil.getConnection();
        PreparedStatement psmt=conn.prepareStatement("insert  into user values(null,'"+bean.getName()+"','"+bean.getPwd()+"');");
        int i=psmt.executeUpdate();
        psmt.close();
        return i;
    }

    public UserBean findsUser(UserBean bean) throws SQLException, IOException, ClassNotFoundException {
        Connection conn= JDBCutil.getConnection();

        String sql="select  * from user where name='"+bean.getName()+"' and pwd='"+bean.getPwd()+"'";
        System.out.println(sql);
        Statement stmt =conn.prepareStatement(sql);
        ResultSet set=stmt.executeQuery(sql);
        UserBean user=null;
        while (set.next()){
            int id=set.getInt("id");
            String  name=set.getString("name");
            user=new UserBean(name,null);
            user.setId(id);
        }
        conn.close();
        stmt.close();
        set.close();
        return user;
    }


}
